package com.hzm.controller;

import com.hzm.bean.User;
import com.hzm.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired(required = false)
    UserService userService;


    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public String insertUser(User user){
        userService.insertUser(user);
        return "/login.jsp";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(User user){
        User user1 = userService.login(user);
        if(user1 != null){
            return "list.mk";
        }else{
            return "login.jsp";
        }
    }

    @RequestMapping("/list")
    public String listUser(Model model){
        List<User> users = userService.listUser();
        model.addAttribute("userList",users);
        return "list.jsp";
    }

    @RequestMapping("/preUpdateUser")
    public String preUpdateUser(@RequestParam("id") Integer id, Model model){
        User user = userService.selectById(id);
        model.addAttribute("user",user);
        return "preUpdate.jsp";
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public String updateUser(User user){
        int i = userService.updateUser(user);
        return "list.mk";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(@RequestParam("id") Integer id){
        int i = userService.deleteUser(id);
        return "list.mk";
    }


}
